package com.linzm.knowledge;

import org.junit.jupiter.api.Test;

import java.util.HashMap;

/**
 * @Author zimingl
 * @Date 2023/1/14 13:31
 * @Description: TODO
 */
public class MapCopy {

    @Test
    public void copy1() {
        HashMap<String, Object> paramMap1 = new HashMap<>();
        paramMap1.put("name", "Marydon");
        paramMap1.put("age", "12");
        HashMap<String, Object> paramMap2;

        //浅拷贝：使用=    paramMap1中的内容发生变化，paramMap2中的内容亦同步发生变化
        paramMap2 = paramMap1;
        paramMap1.remove("name");
        System.out.println(paramMap2); //{age=12}


        HashMap<String, Object> paramMap3 = new HashMap<>();
        paramMap1.put("name", "Marydon");
        paramMap1.put("age", "12");
        HashMap<String, Object> paramMap4 = new HashMap<>();

        //深拷贝：使用Map.putAll()  paramMap1中的内容发生变化，paramMap2中的内容不变
        paramMap4.putAll(paramMap3);
        paramMap1.remove("name");
        System.out.println(paramMap4); //{name=Marydon, age=12}
    }
}
